from datetime import datetime

from scrapy_plus.http.request import Request
from scrapy_plus.middlewares.downloader_middlewares import DownloaderMiddleware
from scrapy_plus.middlewares.spider_middlewares import SpiderMiddleware
from scrapy_plus.utils.logs import logger
from .scheduler import Scheduler
from .downloader import Downloader
from .pipeline import Pipeline
from .spider import Spider


class Engine:
    def __init__(self,spider):
        self.spider = spider
        self.pipeline = Pipeline()
        self.downloader = Downloader()
        self.scheduler = Scheduler()
        self.spider_mid = SpiderMiddleware()  # 初始化爬虫中间件对象
        self.downloader_mid = DownloaderMiddleware()  # 初始化下载器中间件对象
    
    def start(self):
        start = datetime.now()  # 起始时间
        logger.info("开始运行时间：%s" % start)  # 使用日志记录起始运行时间
        self._start_engine()
        stop = datetime.now()  # 结束时间
        logger.info("开始运行时间：%s" % stop)  # 使用日志记录结束运行时间
        logger.info("耗时：%.2f" % (stop - start).total_seconds())  # 使用日志记录运行耗时
        return self._start_engine()
    
    def _start_engine(self):
        start_request = self.spider.start_request()
        
        self.scheduler.add_request(start_request)
        
        request = self.scheduler.get_request()
        
        request = self.downloader_mid.process_request(request)
        
        response = self.downloader.get_response(request)
        
        response = self.downloader_mid.process_response(response)
        
        result = self.spider.parse(response)
        
        if isinstance(result, Request):
            result = self.spider_mid.process_request(result)
            self.scheduler.add_request(result)
        else:
            self.pipeline.process_item(result)
